/*

        ABluetooth - Processing for Android Bluetooth Library

        Copyright (c) 2012 Marlon J. Manrique
        
        http://www.marlonj.com

        This library is free software; you can redistribute it and/or
        modify it under the terms of the GNU Lesser General Public
        License as published by the Free Software Foundation; either
        version 2.1 of the License, or (at your option) any later version.

        This library is distributed in the hope that it will be useful,
        but WITHOUT ANY WARRANTY; without even the implied warranty of
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
        Lesser General Public License for more details.

        You should have received a copy of the GNU Lesser General
        Public License along with this library; if not, write to the
        Free Software Foundation, Inc., 59 Temple Place, Suite 330,
        Boston, MA  02111-1307  USA

	$Id$

*/

package com.marlonj.processing.abluetooth;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

/**
 * Implementation of the bluetooth discovery service 
 */ 
class ABroadcastReceiver extends BroadcastReceiver {
	
	/**
	 * The bluetooth library 
	 */ 
	private ABluetooth bluetooth; 

	/**
	 * Create the receiver associated with the current bluetooth library 
	 * 
	 * @param bluetooth Bluetooth Library 
	 */ 
	public ABroadcastReceiver(ABluetooth bluetooth) {
		this.bluetooth = bluetooth; 
	}

	/**
	 * Receive messages from the bluetooth discover service 
	 * 
	 * @param context 
	 * @param intent 
	 */
	public void onReceive(Context context, Intent intent) {

		// Get the action 
		String action = intent.getAction();

		// If a device was found get the device and dispatch the event 
		if(action.equals(BluetoothDevice.ACTION_FOUND)) {
			// Get the device 
			BluetoothDevice device = 
				intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

			// Dispath the event, encapsulate the device inside own device 
			bluetooth.deviceDiscovered(new ABluetoothDevice(device));
		} else if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)) {
			// Dispath the event
			bluetooth.discoveryEnded();
		}
	}
}
